package com.linghang.learnspringboot.controller;

import com.linghang.learnspringboot.req.EbookQueryReq;
import com.linghang.learnspringboot.req.EbookSaveReq;
import com.linghang.learnspringboot.resp.CommonResp;
import com.linghang.learnspringboot.resp.EbookQueryResp;
import com.linghang.learnspringboot.resp.PageResp;
import com.linghang.learnspringboot.service.EbookService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/ebook")
public class EbookController {
    @Resource
    private EbookService ebookService;
//    Controller层不要出现domain实体

    @GetMapping("/list")
    public CommonResp list(@Valid EbookQueryReq req) {
        CommonResp<PageResp<EbookQueryResp>> resp = new CommonResp<>();
        //CommonResp<Object> resp = new CommonResp<>();
        //这里的泛型是返回实际业务数据的类型，即content类型
        PageResp<EbookQueryResp> list = ebookService.list(req);
//        在CommonResp类里success默认是true，故不再setSuccess
        resp.setContent(list);
        return resp;
    }


    @PostMapping("/save")
    //一般保存更新用PostMapping
    //@RequestBody这个注解对应的是json方式的POST提交
    //###
    //POST http://localhost:80/ebook/save
    //Content-Type: application/json
    //注意：此时用application/json，测试会不成功
    //{}
    //如果使用ptrt，同样是POST，它是以form表单方式传参，不需要添加注解
    //axios的post提交就是application/json，后端需要以@RequestBody接收

    public CommonResp save(@Valid @RequestBody EbookSaveReq req) {
        CommonResp resp = new CommonResp<>();
        ebookService.save(req);
        return resp;
    }


    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        ebookService.delete(id);
        return resp;
    }
}


